package hr.cars.functional.stepdefs;

import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import cucumber.runtime.PendingException;
import cucumber.table.DataTable;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * Created with IntelliJ IDEA.
 * User: VMoskalenko
 * Date: 05.07.12
 * Time: 13:01
 */
public class SeleniumSteps {

    WebDriver driver;

    @When("^I access \"([^\"]*)\" page$")
    public void I_access_page(String service) {
        driver = new HtmlUnitDriver();
        driver.get("http://localhost:8888/hr/" + service);
    }

    @Then("^stuff result page contains$")
    public void stuff_result_page_contains(List<JdbcSteps.Employee> employees) {
        int index = 1;
        for (JdbcSteps.Employee employee : employees) {
            assertThat(fieldValue("fn", index), is(employee.firstName));
            assertThat(fieldValue("ln", index), is(employee.lastName));
            assertThat(fieldValue("jt", index++), is(employee.jobTitle));
        }
    }

    public String fieldValue(String prefix, int index) {
        return driver.findElement(By.id(prefix + index)).getText();
    }


}
